<template>
  <u-toast ref="refGlobalToast"></u-toast>
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";

const refGlobalToast = ref(null);
const globalToastID = ref(0);

onMounted(() => {

  globalToastID.value = getDefultKey();
  uni.setStorageSync("__APP__GLOBAL_TOAST_ID", globalToastID.value);
  uni.$on("showToast", (opt = {}) => {
    console.log("showToast --->>", opt);
    refGlobalToast.value?.show(opt);
  });
});
onBeforeUnmount(() => {
  uni.$off("showToast");
});

/**
 * 创建一个随机 key
 * @param {*} item
 */
const getDefultKey = () => {
  const randomString = Math.random().toString(36).substring(2, 10);
  return `__${randomString}`;
};
</script>
